<?php
namespace xmi_shop\admin\model;
use xmi_shop\model;
class settingModel extends model{

    public static function set( $code, $key, $value ){

        self::delete( $code, $key );

        $sql = "insert into sh_setting set `code`= '" . $code
            . "',`key`='" . $key
            . "',`value`='" . $value . "'";

//        $result = self::$db->query( $sql );
//        return $result->num_rows;
        return pdo_query( $sql );

    }

    public static function delete( $code, $key ){

        $sql = "delete from sh_setting where `code`='". $code
            ."' and `key`='". $key ."' ";
//        $result = self::$db->query( $sql );
//
//        return $result->num_rows;

        return pdo_query( $sql );

    }

    public static function get( $code, $key ){

        $sql = "select * from sh_setting where `code`='". $code
            ."' and `key`='". $key ."' ";
//        $result = self::$db->query( $sql );
//        return !empty($result->row)?$result->row['value']:false;
        $result = pdo_fetch( $sql );
        return !empty( $result ) ? $result : false;
    }

    public static function get_all(){

        $sql = "select * from sh_setting ";
//        $result = self::$db->query( $sql );
//        return $result->rows;
        return pdo_fetchall( $sql );
    }

    public static function set_all( $data ){

        $del_sql = "truncate table sh_setting";
//        self::$db->query( $del_sql );
        pdo_query( $del_sql );

        $sql = "INSERT INTO sh_setting (`code`,`key`,`value`) VALUES ";

        $values = [];
        foreach( $data as $val ){

            $str = "'".$val['code'] ."','" . $val['key'] . "','" . $val['value']."'";
            $values[] = "(" . $str . ")";

        }

        if(!empty( $values ) ){

            $sql .= implode(',', $values );
//            $result = self::$db->query( $sql );
//            return $result->num_rows;
            return pdo_query( $sql );
        }

        return 0;


    }


}